#include "namegiver.h"

#include "database.h"

NameGiver::NameGiver()
{
  foreach(ed::Characteristic c,ed::Characteristic::values()) _characteristicBonuses[c] = 0;
}

int NameGiver::characteristic(ed::Characteristic c) const
{
  Q_ASSERT(c != ed::Char_FlightSpeed || _race == ed::Race_Sylphelin);
  Q_ASSERT(c != ed::Char_FlightRunSpeed || _race == ed::Race_Sylphelin);

  int result = -1;

  // Flight speed for Sylphelins
  if (c == ed::Char_FlightSpeed && _race == ed::Race_Sylphelin) result = Database::characteristicValue(ed::Char_WalkSpeed, attributeValue(ed::Attr_DEX) + 8);
  else if (c == ed::Char_FlightRunSpeed && _race == ed::Race_Sylphelin) result = Database::characteristicValue(ed::Char_RunSpeed, attributeValue(ed::Attr_DEX) + 8);
  if (result != -1) return result; // Avoid to call Database with these two specific characteristics

  int attrVal = attributeValue(Database::characteristicAttribute(c));
  // Specific attribute if asking for speed
  if (c == ed::Char_WalkSpeed || c == ed::Char_RunSpeed) result = Database::characteristicValue(c, attrVal + Database::movementDexterityModifier(_race));
  else result = Database::characteristicValue(c,attrVal);

  return result + characteristicBonus(c);
}

QMap<Skill, int> NameGiver::skills() const
{
  return _skills;
}

int NameGiver::skillLevel(const Skill &s) const
{
  if (s.level().useAttribute())
    return skillRank(s) + attributeLevel(s.level().attribute());
  return skillRank(s);
}

void NameGiver::setSkillRank(const Skill &s, int rank)
{
  if (rank <= 0) _skills.remove(s);
  else _skills[s] = rank;
}

void NameGiver::set_race(ed::Race r)
{
  if (r == ed::Race_Obsidien) _characteristicBonuses[ed::Char_Injury] += 3;
  else if (r == ed::Race_Sylphelin) _characteristicBonuses[ed::Char_PhysicalDefense] += 2;
  _race = r;
}

int NameGiver::characteristicBonus(ed::Characteristic c) const
{
  if (!_characteristicBonuses.contains(c)) return 0;
  return _characteristicBonuses[c];
}

void NameGiver::decreaseLegendPoints(int amount)
{
  _legendPoints-=amount;
  Q_ASSERT(_legendPoints>=0);
}

int NameGiver::karmaLevel() const
{
  return Database::karmaLevel(_race);
}

int NameGiver::maximumKarma() const
{
  return Database::maximumKarma(_race);
}

int NameGiver::skillRank(const Skill &s) const
{
  if (!_skills.contains(s)) return 0;
  return _skills[s];
}
